package com.example.myapplication1.publishFriendCircle;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication1.R;


public class AddPictureRecyclerView<T, VH extends RecyclerView.ViewHolder> extends RecyclerView {

    private AddPictureTouchAdapter<T, VH> mAdapter;
    private ItemTouchHelper itemTouchHelper;
    private RecyItemTouchHelperCallBack<T, VH> callBack;
    private DragListener<T, VH> dragListener;
    private OnRecyclerItemTouchListener<VH> onRecyclerItemTouchListener;
    /**
     * 最大选择数量
     */
    private int maxCount;
    /**
     * 是否可以拖动排序
     */
    private boolean moveEnable;

    private boolean nestedScrollEnable;

    public AddPictureRecyclerView(Context context) {
        this(context, null);
    }

    public AddPictureRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddPictureRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    private void init(@Nullable AttributeSet attrs) {
        TypedArray mTypedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AddPictureRecyclerView);
        maxCount = mTypedArray.getInteger(R.styleable.AddPictureRecyclerView_max_count, 9);
        moveEnable = mTypedArray.getBoolean(R.styleable.AddPictureRecyclerView_move_enable, true);
        nestedScrollEnable = mTypedArray.getBoolean(R.styleable.AddPictureRecyclerView_nested_scroll_enable, false);
        mTypedArray.recycle();
        setNestedScrollingEnabled(nestedScrollEnable);
        setLayoutManager(new GridLayoutManager(getContext(), 3, LinearLayoutManager.VERTICAL, false));
        if (moveEnable) {
            setClipChildren(false);
        }
        initTouchHelper();
    }

    private void initTouchHelper() {
        if (!moveEnable) return;
        initRecyclerItemTouch();
        addOnItemTouchListener(onRecyclerItemTouchListener);
    }

    private void initRecyclerItemTouch() {
        if (null == onRecyclerItemTouchListener) {
            onRecyclerItemTouchListener = new OnRecyclerItemTouchListener<VH>(this) {
                @Override
                public void onItemClick(VH vh) {
                }

                @Override
                public void onItemLongClick(VH vh) {
                    if (null != vh && null != dragListener && mAdapter != null) {
                        if (dragListener.canDrag(vh, vh.getAdapterPosition(), mAdapter.getItem(vh.getAdapterPosition()))) {
                            if (itemTouchHelper != null) {
                                itemTouchHelper.startDrag(vh);
                            }
                        }
                    }
                }
            };
        }
        addOnItemTouchListener(onRecyclerItemTouchListener);
    }

    @Override
    public void setAdapter(@Nullable Adapter adapter) {
        if (!(adapter instanceof AddPictureTouchAdapter)) {
            throw new IllegalArgumentException("Must set AddPictureTouchAdapter");
        }
        mAdapter = (AddPictureTouchAdapter<T, VH>) adapter;
        setMaxCount(maxCount);
        super.setAdapter(adapter);
        setTouchHelperCallBack();
    }

    private void setTouchHelperCallBack() {
        if (!moveEnable) return;
        callBack = new RecyItemTouchHelperCallBack<>(mAdapter);
        setDragListener();
        itemTouchHelper = new ItemTouchHelper(callBack);
        itemTouchHelper.attachToRecyclerView(this);
    }

    private void setDragListener() {
        if (null != callBack) callBack.setDragListener(dragListener);
    }

    public void setDragListener(DragListener<T, VH> dragListener) {
        this.dragListener = dragListener;
        setDragListener();
    }

    public void setItemTouchHelperCallBack(RecyItemTouchHelperCallBack<T, VH> callBack) {
        this.callBack = callBack;
    }

    public RecyItemTouchHelperCallBack<T, VH> getItemTouchHelperCallBack() {
        return callBack;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
        if (mAdapter != null) {
            mAdapter.setMaxCount(maxCount);
        }
    }

    public int getMaxCount() {
        return maxCount;
    }
}
